<?php
/**
 * ENM Web Crawling : Step 3 : Sutdent Load Detail from Web site
 */

use Fox\Dao\SqlFragment;
use Custom\Student\Enm;
use Custom\Student;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Fox\Html\Screen;

set_time_limit(Xview::TIMEOUT_VERY_LONG);

$screen = new Screen("extract","extract");
$block = new Block("b","Extract Source Files");
$block->add(new TextArea("comments",false));
$block->add(new CheckBox("commit"));
$block->add(new CheckBox("extract",false));
$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		$paterns[] = "#&\#8211;#is";
		$replacements[] = "-";
		$paterns[] = "#&\#8217;#is";
		$replacements[] = "'";
						
		$finder = new Enm();
		$students = $finder->find(new SqlFragment("reference IS NOT NULL AND status = 0 "),null,null,$inputs['dbcnt']);
		foreach($students as $student)
		{
			/*
			$toclean = false;
			if($student->fname != $student->infogenerale_prenom || $student->lname != $student->infogenerale_nom)
			{
				push_msg("W","$student->reference : $student->status Issue");
				$toclean = true;
			}
			*/
			if(!$inputs['extract'])
			{
				$macro = "URL GOTO=http://www.mines-saint-etienne.org/gene/generer_identite_xml.php?det=$student->reference<br>";
				$macro .= "SAVEAS TYPE=CPL FOLDER=* FILE=$student->reference<br>";
				echo $macro;
			}
			else
			{
				$fieldsFinal = null;
				$fields = null;
				$doc = null;
				// Load Xml File
				$ref = $student->reference;
				$filename = _TEMP_."ENMSE\Detail\\".$ref.".htm";
				if(file_exists($filename))
				{
					$html = substr(file_get_contents($filename),145);
					$html = preg_replace("/\\n\\r/"," ",substr($html,0,-14));
					$doc = @simplexml_load_string($html);
				}
				if(!is_object($doc))
				{
				
					$fieldsFinal['status'] = 5;
					$filename = "http://www.mines-saint-etienne.org/gene/generer_identite_xml.php?det=".$ref;
					$html = file_get_html($filename);
					var_dump($ref);
					$infogenerale = $html->find("infogenerale");
					if(!isset($infogenerale[0]))
					{
						//try to load from the website
						$file = "http://www.mines-saint-etienne.org/gene/generer_identite_xml.php?det=".$ref;
						$html = file_get_html($file);
						$infogenerale = $html->find("infogenerale");
					}
					if(!isset($infogenerale[0]))
					{
						push_msg("E","Unable to find a correct file for $ref");
						continue;
					}
					foreach($infogenerale[0]->children as $children)
					{
						$fieldname = "infogenerale_".$children->tag;
						$value = trim(utf8_decode($children->innertext));
			
						$fields[$fieldname] = preg_replace($paterns,$replacements,$value);
					}
					$infostatut = $html->find("infostatut");
					foreach($infostatut[0]->children as $children)
					{
						$fieldname = "infostatut_".$children->tag;
						$value = trim(utf8_decode($children->innertext));
						$fields[$fieldname] = preg_replace($paterns,$replacements,$value);
					}
					$adresseprincipale = $html->find("adressesperso adresseprincipale");
					foreach($adresseprincipale[0]->children as $children)
					{
						$fieldname = "adresseprincipale_".$children->tag;
						$value = trim(utf8_decode($children->innertext));
						$fields[$fieldname] = preg_replace($paterns,$replacements,$value);
					}
					$adressesecondaire = $html->find("adressesperso adressesecondaire");
					foreach($adressesecondaire[0]->children as $children)
					{
						$fieldname = "adressesecondaire_".$children->tag;
						$value = trim(utf8_decode($children->innertext));
						$fields[$fieldname] = preg_replace($paterns,$replacements,$value);
					}
					$adressespro = $html->find("adressespro adresseprincipale");
					foreach($adressespro[0]->children as $children)
					{
						$fieldname = "adressespro_".$children->tag;
						$value = trim(utf8_decode($children->innertext));
						$fields[$fieldname] = preg_replace($paterns,$replacements,$value);
					}
					
				}
				
				if(is_object($doc))
				{
					$fields['infogenerale_nom'] = (string) $doc->infoperso->infogenerale->nom;
					$fields['infogenerale_prenom'] = (string) $doc->infoperso->infogenerale->prenom;
					$fields['infogenerale_promo'] = (string) $doc->infoperso->infogenerale->promo;
					$fields['infogenerale_photo'] = (string) $doc->infoperso->infogenerale->photo;
					if($fields['infogenerale_photo'] == 'vide')
					{
						unset($fields['infogenerale_photo']);
					}
					$fields['infogenerale_minicv'] =(string) $doc->infoperso->infogenerale->minicv;
					$fields['infogenerale_cv'] = (string) $doc->infoperso->infogenerale->cv;
					if($fields['infogenerale_cv'] == 'vide')
					{
						unset($fields['infogenerale_cv']);
					}
					$fields['infogenerale_ecole'] = (string) $doc->infoperso->infogenerale->ecole;
					$fields['infostatut_diplome'] = (string) $doc->infoperso->infostatut->diplome;
					$fields['infostatut_decoration'] = (string) $doc->infoperso->infostatut->decoration;
					$fields['infostatut_delegue'] = (string) $doc->infoperso->infostatut->delegue;
					$fields['infostatut_conseil'] = (string) $doc->infoperso->infostatut->conseil;
					$fields['infostatut_cotisant'] = (string) $doc->infoperso->infostatut->cotisant;
					$fields['infostatut_poste'] = (string) $doc->infoperso->infostatut->poste;
					$fields['infostatut_mailavie'] = (string) $doc->infoperso->infostatut->mailavie;
				
					$fields['adresseprincipale_adresse'] = (string) $doc->adressesperso->adresseprincipale->adresse;
					$fields['adresseprincipale_codepostal'] = (string) $doc->adressesperso->adresseprincipale->codepostal;
					$fields['adresseprincipale_ville'] = (string) $doc->adressesperso->adresseprincipale->ville;
					$fields['adresseprincipale_pays'] = (string) $doc->adressesperso->adresseprincipale->pays;
					$fields['adresseprincipale_tel'] = (string) $doc->adressesperso->adresseprincipale->tel;
					$fields['adresseprincipale_mob'] = (string) $doc->adressesperso->adresseprincipale->mob;
					$fields['adresseprincipale_fax'] = (string) $doc->adressesperso->adresseprincipale->fax;
					$fields['adresseprincipale_mail'] = (string) $doc->adressesperso->adresseprincipale->mail;
					$fields['adresseprincipale_site'] = (string) $doc->adressesperso->adresseprincipale->site;
					
					$fields['adressesecondaire_adresse'] = (string) $doc->infoperso->adressesecondaire->adresse;
					$fields['adressesecondaire_codepostal'] = (string) $doc->infoperso->adressesecondaire->codepostal;
					$fields['adressesecondaire_ville'] = (string) $doc->infoperso->adressesecondaire->ville;
					$fields['adressesecondaire_pays'] = (string) $doc->infoperso->adressesecondaire->pays;
					$fields['adressesecondaire_tel'] = (string) $doc->infoperso->adressesecondaire->tel;
					$fields['adressesecondaire_mob'] = (string) $doc->infoperso->adressesecondaire->mob;
					$fields['adressesecondaire_fax'] = (string) $doc->infoperso->adressesecondaire->fax;
					$fields['adressesecondaire_mail'] = (string) $doc->infoperso->adressesecondaire->mail;
					$fields['adressesecondaire_site'] = (string) $doc->infoperso->adressesecondaire->site;
					
					$fields['adressespro_entreprise'] = (string) $doc->adressespro->adresseprincipale->entreprise;
					$fields['adressespro_adresse'] = (string) $doc->adressespro->adresseprincipale->adresse;
					$fields['adressespro_codepostal'] = (string) $doc->adressespro->adresseprincipale->codepostal;
					$fields['adressespro_ville'] = (string) $doc->adressespro->adresseprincipale->ville;
					$fields['adressespro_pays'] = (string) $doc->adressespro->adresseprincipale->pays;
					$fields['adressespro_tel'] = (string) $doc->adressespro->adresseprincipale->tel;
					$fields['adressespro_mob'] = (string) $doc->adressespro->adresseprincipale->mob;
					$fields['adressespro_fax'] = (string) $doc->adressespro->adresseprincipale->fax;
					$fields['adressespro_mail'] = (string) $doc->adressespro->adresseprincipale->mail;
					$fields['adressespro_site'] = (string) $doc->adressespro->adresseprincipale->site;
				}
				if(is_array($fields))
				{
					foreach($fields as $fieldname => $value)
					{
						if(!empty($value))
						{
							$fieldsFinal[$fieldname] = trim(utf8_decode($value));
							if($fieldname == 'adresseprincipale_mail' || 
								$fieldname == 'adresseprincipale_mob' ||
								$fieldname == 'adresseprincipale_tel' ||		
								$fieldname == 'adresseprincipale_fax' ||
								$fieldname == 'adressespro_mail' ||
								$fieldname == 'adressespro_mob'  ||
								$fieldname == 'adressespro_site' ||
								$fieldname == 'adressespro_tel'  || 
								$fieldname == 'adressespro_fax'  
								)
							{
								if(substr($fieldsFinal[$fieldname],0,2) == ': ')
									$fieldsFinal[$fieldname] = substr($fieldsFinal[$fieldname],2);
							}
						}
						else 
						{
							$fieldsFinal[$fieldname] = ' ';
						}
					}
					$fieldsFinal['status'] = 2;
					try {
						push_msg("I","$ref Traited");
						$student->setFields($fieldsFinal);
						if($inputs['commit'])
							$student->update();
					}
					catch (Fox\Object\Exception\Save $e)
					{
						push_msg("E",$e->getMessage());
					}
				}
			}
		}
	}
}
echo $screen->display();
